package com.data4.supplier.controller;


import com.data4.feignapi.common.constants.BrandConstants;
import com.data4.feignapi.common.constants.GatewayConstants;
import com.data4.feignapi.dto.BrandDTO;
import com.data4.feignapi.dto.Result;
import com.data4.supplier.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/brand")
@Tag(name = "商标管理")
public class BrandController {
    @Resource
    private BrandService brandService;

    @PostMapping("/addBrand")
    @Operation(summary = "添加商标")
    public Result addBrand(@RequestBody BrandDTO brandDTO,
                           @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return brandService
                .addBrand(brandDTO, Integer.parseInt(supplierId));
    }

    @PutMapping("/update/{brandId}")
    @Operation(summary = "修改商标")
    public Result update(@RequestBody BrandDTO brandDTO, @PathVariable Integer brandId,
                         @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return brandService.updateBrand(brandDTO, brandId, Integer.parseInt(supplierId));
    }

    /**
     * 用于首页返回查询
     *
     * @param current
     * @param num
     * @return
     */
    @GetMapping("/getBrandByTime/{current}/{num}")
    @Operation(summary = "根据时间查询商标")
    public Result getBrandByTime(@PathVariable Integer current, @PathVariable Integer num,
                                 @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return brandService.getBrandByTime(current, num, Integer.parseInt(supplierId));
    }

    @GetMapping("/getBrandByName/{current}/{num}")
    @Operation(summary = "模糊查询商标", description = "输入的只要还有即可查")
    public Result getBrandByName(@PathVariable Integer current,
                                 @PathVariable Integer num,
                                 @RequestParam("brandName") String brandName,
                                 @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return brandService.getBrandByNameLike(brandName, current, num, Integer.parseInt(supplierId));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商标")
    public Result delete(@RequestBody List<Long> brandIdList,
                         @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return brandService.removeBrands(brandIdList, Integer.parseInt(supplierId)) ? Result.ok(BrandConstants.DELETE_SUCCESS)
                : Result.fail(BrandConstants.DELETE_FAIL);
    }
}

